<!-- 学期编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改学期':'添加学期'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">

      <el-form-item label="所属学校:" prop="organization">
          <el-select
            filterable
            clearable
            v-model="form.organization"
            @change="changeGradeList"
            size="small"
            placeholder="-请选择所属学校-"
            class="ele-block">
            <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>

      <el-form-item label="所属年级:" prop="grade">
          <el-select
            multiple
            filterable
            clearable
            v-model="form.grade"
            @change="$forceUpdate()"
            size="small"
            placeholder="-请选择年级-"
            class="ele-block">
            <el-option v-for="item in gradeList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>

      <el-form-item
        label="学期名称:"
        prop="name">
        <el-input
          :maxlength="20"
          v-model="form.name"
          placeholder="请输入学期名称"
          clearable/>
      </el-form-item>

      <el-form-item
        label="开始日期:"
        prop="start">
        <el-date-picker
          :maxlength="20"
          type="date"
          v-model="form.start"
          value-format="yyyy-MM-dd"
          placeholder="请输入学期开始日期"
          clearable/>
      </el-form-item>

      <el-form-item
        label="结束日期:"
        prop="end">
        <el-date-picker
          :maxlength="20"
          type="date"
          v-model="form.end"
          value-format="yyyy-MM-dd"
          placeholder="请输入学期结束日期"
          clearable/>
      </el-form-item>

      <el-form-item label="显示顺序:" prop="sort">
        <el-input-number
          :min="0"
          v-model="form.sort"
          placeholder="请输入显示顺序"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>

      <el-form-item label="备注:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.note"
          placeholder="请输入备注"/>
      </el-form-item>
          </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
// import uploadImage from '@/components/uploadImage'

export default {
  name: 'SemesterEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              organization: [
          {required: true, message: '请输入所属学校', trigger: 'blur'}
        ],
                  grade: [
          {required: true, message: '请输入所属年级', trigger: 'blur'}
        ],
                  name: [
          {required: true, message: '请输入学期名称', trigger: 'blur'}
        ],
                  start: [
          {required: true, message: '请输入学期开始日期', trigger: 'blur'}
        ],
                  end: [
          {required: true, message: '请输入学期结束日期', trigger: 'blur'}
        ],
                  sort: [
          {required: true, message: '请输入显示顺序', trigger: 'blur'}
        ],
                  note: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 学院列表
      schoolList: [],
      // 年级列表
      gradeList: [],
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        // 获取学院列表
        this.getSchoolList();
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    // 获取学院列表
    this.getSchoolList();
    // 获取年级列表
    // this.getGradeList();
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/semester/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /**
     * 获取组织列表
     */
    getSchoolList() {
      if(this.form.organization){
        // this.form.superior = null;
        this.$forceUpdate();
        this.getGradeList();
      }
      this.$http.get('/school/getSchoolList').then(res => {
        if (res.data.code === 0) {
          this.schoolList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },

    /**
     * 获取年级列表
     */
    getGradeList() {
      if(this.form.organization){
        // this.form.grade = null;
        this.$forceUpdate();
      }
      this.$http.post('/grade/getGradeList',{oid:this.form.organization}).then(res => {
        if (res.data.code === 0) {
          this.gradeList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取年级列表
     */
    changeGradeList() {
      this.form.grade = null;
      this.getGradeList();
    },
  }
}
</script>

<style scoped>
</style>
